|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
In the Customers form, you again use the chameleon Command Button technique covered earlier in the book, permitting the four Command Buttons to do the work of six. When the user views or edits records in the default state, the global variable EnteringRecords is False and the first two Command Buttons have the captions Add New and Delete Current. When the user clicks on the Add New button, EnteringRecords is toggled to True, the Add New buttons caption is changed to Save, and the Delete Current buttons caption is changed to Cancel. When the user clicks on Save or Cancel after entering the new record, the process is reversed. While the user is entering a new record, you also want the List and Finished Command Buttons and the Data control to be disabled. As for the database itself, the commands are fairly simple and consist of methods belonging to the RecordSet object. The RecordSet object is associated with the ADO Data control and is automatically created when the control connected to a data source and a query is run. Here are the methods youll use:
As for the List All command, the capabilities of the DataGrid control make it simple to code. You only have to set the properties of the ADO Data control to which the DataGrid is bound so they point at the same database and table as the Customer forms ADO Data control. Then, refresh the ADO Data control, and use the Show method to display the form. Heres the code: frmList.Adodc1.ConnectionString = Adodc1.ConnectionString frmList.Adodc1.RecordSource = Adodc1.RecordSource frmList.Caption = Customers Table frmList.Adodc1.Refresh frmList.Show The full Command1_Click event procedure for the Customers form is shown in Listing 22.2. Listing 22.2 The Command1_Click event procedure in CUSTOMER.FRM.
Private Sub Command1_Click(Index As Integer)
Dim Reply As Integer
Select Case Index
Case 0 Add New or Save.
If EnteringRecord Then Save command.
Save the new record.
Adodc1.Recordset.Update
EnteringRecord = False
Change command buttons.
Command1(0).Caption = &Add New
Command1(1).Caption = &Delete Current
Enable the data control.
Adodc1.Enabled = True
Adodc1.Recordset.Requery
Adodc1.Recordset.MoveLast
Enable List and Finished buttons.
Command1(2).Enabled = True
Command1(3).Enabled = True
Else Add New command.
EnteringRecord = True
Put focus in first text box. This is actually the
second TextBox as the first one (CustID) is locked.
Text1(1).SetFocus
Disable List and Finished buttons.
Command1(2).Enabled = False
Command1(3).Enabled = False
Change command buttons.
Command1(0).Caption = &Save
Command1(1).Caption = &Cancel
Disable the data control.
Adodc1.Enabled = False
Add a new record.
Adodc1.Recordset.AddNew
End If
Case 1 Delete current or Cancel.
If EnteringRecord Then Cancel command.
Reply = MsgBox(Discard current entry?, vbYesNo + vbQuestion, _ Cancel Entry)
If Reply = vbNo Then Exit Sub
Adodc1.Recordset.CancelUpdate
Adodc1.Refresh
EnteringRecord = False
Change button captions.
Command1(0).Caption = &Add New
Command1(1).Caption = &Delete Current
Enable List and Finished buttons.
Command1(2).Enabled = True
Command1(3).Enabled = True
Enable ADO Data control.
Adodc1.Enabled = True
Else Delete Current command.
Reply = MsgBox(Delete this record?, vbYesNo + _
vbQuestion, Cancel Entry)
If Reply = vbNo Then Exit Sub
Adodc1.Recordset.Delete
Adodc1.Recordset.MoveNext
End If
Case 2 List all.
frmList.Adodc1.ConnectionString = Adodc1.ConnectionString
frmList.Adodc1.RecordSource = Adodc1.RecordSource
frmList.Caption = Customers Table
frmList.Adodc1.Refresh
frmList.Show
Case 3 Finished.
Hide
End Select
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express w |